﻿<cfscript>
/** 
* @hint "acadmicBase - Dictionary"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.dictionaryListener" 
	output=false 
	{
		
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 

	/**
	* @hint "返回行政区划列表"
	*/	
	public function getChinaArea() output=false {
		
		var sql = "SELECT area_code, area_name FROM t_dict_china_area ORDER BY area_code ASC ";
		
		var queryObj = new Query( datasource=application.dnsSlave );
		var rs_area = queryObj.execute( sql=sql ).getResult(); 
	
		areaArray = arrayNew(1);
		
		for( var i=1; i LTE rs_area.recordCount; i++){
			arrayAppend(areaArray,[rs_area["area_code"][i],rs_area["area_name"][i]]);
		}
		
		return areaArray;
	}
	
	/**
	*@hint "添加数据字典"
	*/
	public void function addDictionary() output=false {
		
		var dictName = event.getArg("dictName");
		var fieldId = event.getArg("dict");
		
		var sql = "";
		var queryObj = "";
		
		
		/* Every SQL operation is TRANSACATION*/
		transaction{
			
			/*检查信息是否存在*/
			sql = "select * from i_code_info where field_id = :fieldId and name = :name";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="fieldId", value=fieldId, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="name", value=dictName, cfsqltype="cf_sql_varchar" );
			
			rs_code = queryObj.execute( sql=sql ).getResult();
			
			if( rs_code.recordCount eq 0){
			
				sql = "select code_id from (select code_id from i_code_info order by code_id desc ) where rownum = 1";	
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				rs_code = queryObj.execute( sql=sql ).getResult();
				
				var id = rs_code.code_id + 1;
				
				
				sql = "INSERT INTO i_code_info(
						code_id, field_id, code, name, gradation
					)VALUES(
						:codeId, :fieldId, '', :name, :gradation  
					)";
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="codeId", value=id, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="fieldId", value=fieldId, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="name", value=dictName, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="gradation", value=id, cfsqltype="cf_sql_integer" );
				
				queryObj.execute( sql=sql );
				
			}
			
			event.setArg("dict", fieldId);
		}
	}
	
	
	/**
	*@hint "修改数据字典"
	*/
	public void function updateDictionary() output=false {
		
		var dictName = event.getArg("dictName");
		var fieldId = event.getArg("dict");
		var codeId = event.getArg("codeId");
		
		var sql = "";
		var queryObj = "";
		
		
		/* Every SQL operation is TRANSACATION*/
		transaction{
			
			sql = "UPDATE i_code_info SET name = :name WHERE code_id = :codeId";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="codeId", value=codeId, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="name", value=dictName, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
		
			event.setArg("dict", fieldId);
			
		}
		
	}
	
	/**
	*@hint "删除数据字典"
	*/
	public void function deleteDictionary() output=false {
		
		var fieldId = event.getArg("dict");
		var codeId = event.getArg("codeId");
		
		var sql = "";
		var queryObj = "";
		
		
		/* Every SQL operation is TRANSACATION*/
		transaction{
			
			sql = "DELETE FROM i_code_info WHERE code_id = :codeId";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="codeId", value=codeId, cfsqltype="cf_sql_integer" );
			
			queryObj.execute( sql=sql );
		
			event.setArg("dict", fieldId);
			
		}
		
	}
	
}

</cfscript>